<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Event Controller.
 * 
 * @class     Event
 * @author    Iqbal
 * @version   1.0.0
 * @copyright Copyright (c) 2014 IPPI (http://www.ippi.or.id)
 */
class event extends CI_Controller {
    /**
	 * Constructor.
	 */
    function __construct() {
        parent::__construct();
        
        // Load model
        $this->load->model('model_event', 'm_event', TRUE);
    }
    
    /**
	 * Load Event Data function.
     * 
     * @param[in] int $offset Offset of Table Event data
     * 
     * @return AJAX String
	 */
    function event_data($offset=0){
        // Set variables
        $limit          = get_option('events_limit');
        $condition      = '';
        $calendar       = '';
        $month_name     = '';
        $this_year      = date('Y');
        $date           = time();
        
        // Set Month Number and Month Name
        $month_list     = array(
            'Januari'   => '01', 
            'Februari'  => '02', 
            'Maret'     => '03', 
            'April'     => '04', 
            'Mei'       => '05', 
            'Juni'      => '06', 
            'Juli'      => '07',
            'Agustus'   => '08', 
            'September' => '09', 
            'Oktober'   => '10', 
            'Nopember'  => '11', 
            'Desember'  => '12'
        );
        $month_num      = date('m', $date);
        $month_name     = array_search ($month_num, $month_list);
        
        // Search Event
        $search_val     = trim( ippi_isset($this->input->get('search_val'), '') );
        if( !empty($search_val) ) {
            $f          = '(%judul% LIKE "%%s%%" OR %content% LIKE "%%s%%")';
            $condition .= str_replace('%s%', $search_val, $f);
        }
        
        // Filter Event
        $filter         = ippi_isset($this->input->get('filter_val'), '');
        if( !empty($filter) ) {
            $filter_by  = ippi_isset($this->input->get('filter_by'), '');
            $and        = ( !empty($search_val) ? ' AND ' : '' );
            
            if( $filter_by == 'archive' ){
                $month_num      = sprintf("%02s", $month_list[$filter]);
                $month_name     = $filter;
                $filter         = $this_year . '-' . $month_num;
                $f              = $and . '%eventdate% LIKE "%s%%"';
                $condition     .= str_replace('%s%', $filter, $f);
            }else{
                $month_num      = substr(trim($filter), 0, 2);
                $month_name     = array_search ($month_num, $month_list);
                $filter         = $this_year . '-' . trim($filter);
                $f              = $and . '%eventdate% LIKE "%s%%"';
                $condition     .= str_replace('%s%', $filter, $f);
            }
        }
        
        // Get Events data
        $events         = $this->m_event->get_all_events_data($limit, $offset, $condition);
        $event          = '';
        $pagination     = '';
        
        // Check response of get events data
        if( !empty($events) ){
            $total      = ippi_get_last_found_rows();
            
            foreach($events as $row){
                $event .= '<li>
                    <div class="caption item-inner">
                        <ul class="ul-horizontal">
                            <li>
                                <div class="badge-circle"><i class="glyphicon icon-calendar"></i></div>
                                <time class="time-heading">'. get_date($row->eventdate) . '</time>
                            </li>
                            <li>
                                <a href="' . base_url() . "event/eventdetails/" . $row->id_event.'" class="h4 event-details" data-id="'.$row->id_event.'">
                                    '. ( strlen($row->judul) > 32 ? rtrim(substr($row->judul, 0, 32)) . "..." : $row->judul ). '
                                </a>
                                <dl class="dl-inline">
                                    <dt>Time:</dt>
                                    <dd>' . get_time($row->eventdate) . '</dd>
                                    <dd class="address"><label>Place:</label> '.$row->place.' - '.$row->state_name.'</dd>
                                </dl>
                            </li>
                            <li style="display: none;"></li>
                        </ul>
                    </div>
                </li>';
            }
            
            // Set Pagination
            if ($total > $limit){
                $this->load->library('pagination');
                
                $config['base_url']     = 'event/event_data/';
                $config['total_rows']   = $total;
                $config['per_page']     = $limit; 
                $config['anchor_class'] = 'class="pagination_event" ';
                
                $this->pagination->initialize($config);
                $pagination             = $this->pagination->create_links(); 
            }
        }else{
            $event .= '
            <div class="show-more">
                <div class="show-more-inner text-center">
                    <a href="#" class="btn-showmore btn btn-primary btn-sm disabled">No Events</a>
                </div>
            </div>';
        }
        
        // Set Calendar
        // =================================================================
        $day            = date('d', $date); 
        $month          = $month_num;
        $year           = date('Y', $date);
        
        //Here we generate the first day of the month 
        $first_day      = mktime(0,0,0,$month, 1, $year) ; 

        //This gets us the month name 
        $month_title    = date('M', $first_day) ;

        //Here we find out what day of the week the first day of the month falls on 
        $day_of_week    = date('D', $first_day) ; 

        //Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
        switch($day_of_week){ 
            case "Sun": $blank = 0; break; 
            case "Mon": $blank = 1; break; 
            case "Tue": $blank = 2; break; 
            case "Wed": $blank = 3; break; 
            case "Thu": $blank = 4; break; 
            case "Fri": $blank = 5; break; 
            case "Sat": $blank = 6; break; 
        }

        //We then determine how many days are in the current month
        $days_in_month  = cal_days_in_month(0, $month, $year) ;
        
        //This counts the days in the week, up to 7
        $day_count      = 1;
        
        //sets the first day of the month to 1 
        $day_num        = 1;
        
        // Return Calendar HTML
        $calendar .= '
        <time datetime="'. date("Ym",$date) . '" class="calendar-month text-time">
            <span class="h2">'. $month_title . '</span><span>'. date("Y",$date) .'</span>
        </time>
        <table>
            <thead>
                <tr>
                    <th scope="col" title="Monday">Mon</th>
                    <th scope="col" title="Tuesday">Tue</th>
                    <th scope="col" title="Wednesday">Wed</th>
                    <th scope="col" title="Thursday">Thu</th>
                    <th scope="col" title="Friday">Fri</th>
                    <th scope="col" title="Saturday">Sat</th>
                    <th scope="col" title="Sunday">Sun</th>
                </tr>
            </thead>
            <tbody>
                <tr>';

                //first we take care of those blank days
                while ( $blank > 0 ) 
                { 
                    $calendar .= "<td></td>"; 
                    $blank = $blank - 1; 
                    $day_count++;
                } 

                //count up the days, untill we've done all of them in the month
                while ( $day_num <= $days_in_month ) 
                {
                    $event_date     = $year . '-' . $month . '-' . sprintf("%02s", $day_num);
                    $event_count    = get_events_by_day($event_date);
                    $event_title    = ( $event_count > 1 ? 'There is '.$event_count.' events' : 'There is '.$event_count.' event');
                    
                    if($event_count > 0){
                        $calendar .= '
                        <td>
                            <a href="#" title="'.$event_title.'" data-toggle="tooltip" data-container="body" class="event_date">
                                <span class="event-date">'.$day_num.'</span>
                                <input class="event-date-val" type="hidden" name="event_date" value="'.$month .'-'. sprintf("%02s", $day_num) .'" />
                            </a>
                        </td>';
                    }else{
                        $calendar .= '<td><span class="event-date">'.$day_num.'</span></td>'; 
                    }
                    
                    $day_num++; 
                    $day_count++;
                    
                    //Make sure we start a new row every week
                    if ($day_count > 7)
                    {
                        $calendar .= "</tr><tr>";
                        $day_count = 1;
                    }
                }

                //Finaly we finish out the table with some blank details if needed
                while ( $day_count >1 && $day_count <=7 ) 
                { 
                    $calendar .= "<td> </td>"; 
                    $day_count++;
                } 
                
                $calendar .= '
                </tr>
            </tbody>
        </table>';
        
        // Set JSON data
        $data = array(
            'events'            => $event,
            'pagination_events' => $pagination,
            'calendar'          => $calendar,
            'month'             => $month_name,
        );
        
        // JSON encode data
        die(json_encode($data));
    }
    
    /**
	 * Load Event Details function.
     * 
     * @param[in] int $id Event ID
     * 
     * @return AJAX String
	 */
    function eventdetails($id=0){
        // Get Event Data
        $event = $this->m_event->get_eventdata($id);
        // Check for response get event
        if( !empty($event) ){
            // Get Event Images
            $event_image    = $this->m_event->get_event_image($id);
            // Set Data View
            $data           = array(
                'event'         => $event,
                'event_id'      => $id,
                'event_image'   => $event_image
            );
            // Set View
            $this->load->view('front/welcome/welcome_events_details',$data);
        }
    }
}

/* End of file event.php */
/* Location: ./application/controllers/event.php */